var currentPage = 1;
var totalPage = 1;
var pageSize = 10;


function pageInfo() {
    //清空
    $(".table-page tr:gt(0) td").html("&nbsp;");
    //声明json串，并赋值
    let jsonA = {
        currentPage: currentPage,
        pageSize: pageSize,
    };
    $.ajaxSettings.async = false;
    $.getJSON("/si/sle", jsonA, function (dataA) {
        console.log(dataA);
        // 总页数
        totalPage = dataA.pages;
        //渲染数据
        $(dataA.list).each(function (i, v) {
            console.log(v);
            // let state = v.state;
            // if (state == 0) {
            //     state = "审核中";
            // } else if (state == -1) {
            //     state = "不通过";
            // } else {
            //     state = "通过"
            // }

            let date = new Date(v.createDate);
            var m = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
            var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
            var h = (date.getHours())
            var mi = (date.getMinutes())
            var s = (date.getSeconds())


            $(".table-page").find("tr").eq(i + 1).find("td").eq(0).text((currentPage - 1) * pageSize + i + 1);
            $(".table-page").find("tr").eq(i + 1).find("td").eq(1).html('<a href="javascript:void(0)" onclick="orderby(' + v.id + ',' + v.user.id + ',\'' + v.conetxt + '\')"   >' + v.title + '</a>');
            $(".table-page").find("tr").eq(i + 1).find("td").eq(2).text(v.user.name);
            $(".table-page").find("tr").eq(i + 1).find("td").eq(3).text(date.getFullYear() + "-" + m + "-" + d + " " +h +":"+mi+":"+s);
        })
    })

    $.ajaxSettings.async = true;
    showPage();

}

//查看详情跳转
function orderby(id, uid, cenetxt) {
    let dengLuId = $("#u_id").val()
    console.log(id + "--------------" + uid + "----------" + cenetxt);
    if (dengLuId == uid) {
        layer.msg(cenetxt, {
            btn: ['删除', '返回'],
            area: ['300px', '300px'],
            btnAlign: 'c',
            time: 0,
            yes: function (index) {
                del(id, uid);
            }
        })
    } else {
        layer.msg(cenetxt, {
            btn: ['返回'],
            area: ['300px', '300px'],

            btnAlign: 'c',
            time: 0,
            yes: function (index) {
                layer.close(index)
            }
        })
    }

}

//展示页码
function showPage() {
    $(".showPage").text("第" + currentPage + "页 / 共" + totalPage + "页");
    if (totalPage < currentPage) {
        currentPage = totalPage;
        pageInfo();
    }
}


// /下一页

function nexts() {
    currentPage++;
    if (currentPage > totalPage) {
        currentPage = totalPage;
        return;
    }
    pageInfo();
}


//上一页
function prevs() {
    currentPage--;
    if (currentPage < 1) {
        currentPage = 1;
        return;
    }
    pageInfo();
}

//首页
function starts() {
    currentPage = 1;
    pageInfo();
}

//尾页
function ends() {
    currentPage = totalPage;
    pageInfo();
}

function a() {
    location.href = "/add";

}

//删除
function del(id, uid) {
    layer.confirm("确认删除吗",{
        btn:['确认','取消'],
        btnAlign:'c',
        yes:function () {
            $.ajaxSettings.async = false;
            $.post("/si/upd", "id=" + id, function (data) {
                if (data) {
                    layer.msg("删除成功！")
                } else {
                    layer.msg("删除失败！")
                }
            })
            $.ajaxSettings.async = true;
            pageInfo();
        }
    })
}

function za() {
    location.href= "/to/zym-tianjia";
}